package com.cby.android.olive.utils

import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.*

/**
 * Description:DateUtils
 *
 * @author 陈宝阳
 * @create 2019/5/20 17: 18
 */
object DateUtils {

    /**
     * 用来判断当前是否还是在有效期内
     *
     * @throws ParseException
     */
    @JvmStatic
    fun isValidity(day: String?): Boolean {
        val sdf = SimpleDateFormat("yyyy-MM-dd HH:mm")

        // 当前的时刻
        val pre = Calendar.getInstance()
        val predate = Date(System.currentTimeMillis())
        pre.time = predate

        // 设定的时刻
        val cal = Calendar.getInstance()
        var date: Date? = null
        try {
            date = sdf.parse(day)
        } catch (e: ParseException) {
            e.printStackTrace()
        }
        cal.time = date

        //用截止日期减去当前日期
        if (cal[Calendar.YEAR] == pre[Calendar.YEAR]) {
            val diffDay = cal[Calendar.DAY_OF_YEAR] - pre[Calendar.DAY_OF_YEAR]
            val diffHour = cal[Calendar.HOUR_OF_DAY] - pre[Calendar.HOUR_OF_DAY]
            val diffMin = cal[Calendar.MINUTE] - pre[Calendar.MINUTE]
            if (diffDay == 0) {
                if (diffHour == 0) {
                    if (diffMin > 5) {
                        //有效期的截止时间提前五分钟
                        return true
                    }
                } else if (diffHour > 0) {
                    //说明还在有效期内
                    return true
                }
            } else if (diffDay > 0) {
                //说明还在有效期内
                return true
            }
        } else if (cal[Calendar.YEAR] > pre[Calendar.YEAR]) {
            //说明还在有效期内
            return true
        }
        return false
    }

    /**
     * 获取截止的时间（根据当前的时间加上天数）
     *
     * @param expireTime 时间单位是秒
     */
    @JvmStatic
    fun getDeadline(expireTime: String): String {
        //Date currentTime = new Date();
        //long newDateTime = currentTime.getTime() + 3600000 * 24 * days;
        val newDate = Date(expireTime.toLong() * 1000 - 15 * 60 * 1000)
        val formatter = SimpleDateFormat("yyyy-MM-dd HH:mm")
        return formatter.format(newDate)
    }

    /**
     * 判断当前时间是否是[8,22]  8点到22点的时间区间内，表示玩家可以完，其他的时间不能玩游戏
     */
    @JvmStatic
    val isBetween8And22: Boolean
        get() {
            val format = "HH:mm:ss"
            val sf = SimpleDateFormat("HH:mm:ss")
            val now = sf.format(Date())
            val nowTime: Date
            var runFlag = false
            try {
                nowTime = SimpleDateFormat(format).parse(now)
                val startTime = SimpleDateFormat(format).parse("08:00:00")
                val endTime = SimpleDateFormat(format).parse("22:00:00")
                runFlag = isEffectiveDate(nowTime, startTime, endTime)
            } catch (e: ParseException) {
                e.printStackTrace()
            }
            return runFlag
        }

    /**
     * 判断当前时间是否在[startTime, endTime]区间，注意时间格式要一致
     *
     * @param nowTime 当前时间
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @author jqlin
     */
    @JvmStatic
    fun isEffectiveDate(nowTime: Date, startTime: Date, endTime: Date): Boolean {
        if (nowTime.time == startTime.time
            || nowTime.time == endTime.time
        ) {
            return true
        }
        val date = Calendar.getInstance()
        date.time = nowTime
        val begin = Calendar.getInstance()
        begin.time = startTime
        val end = Calendar.getInstance()
        end.time = endTime
        return date.after(begin) && date.before(end)
    }

    /**
     * 判断剩余的秒数，是否大于（提前的秒数[remainTime]）
     * @param remainTime Int
     * @return Boolean
     */
    @JvmStatic
    fun leftTimeSeconds(remainTime: Int): Boolean {
        val c = Calendar.getInstance()
        val nowSecond = c.timeInMillis / 1000
        //设置需要的时间
        val midnight = Calendar.getInstance()
        midnight.time = Date()
        midnight[Calendar.HOUR_OF_DAY] = 22
        midnight[Calendar.MINUTE] = 0
        midnight[Calendar.SECOND] = 0
        midnight[Calendar.MILLISECOND] = 0
        val limitSeconds = midnight.time.time / 1000
        val leftTime = (limitSeconds - nowSecond).toInt()
        CbyLogUtils.i(
            "m3839",
            "limitSeconds = $limitSeconds, nowSecond = $nowSecond, leftTime = $leftTime, remainTime = $remainTime"
        )
        return leftTime >= remainTime
    }

    /**
     * 判断剩余的秒数，是否大于（提前的秒数[remainTime]）
     * @param remainTime Int
     * @return Boolean
     */
    @JvmStatic
    fun leftTimeSeconds2(remainTime: Int): Boolean {
        val format = "HH:mm:ss"
        val sf = SimpleDateFormat("HH:mm:ss")
        val now = sf.format(Date())
        val nowTime: Date
        try {
            nowTime = SimpleDateFormat(format).parse(now)
            val endTime = SimpleDateFormat(format).parse("22:00:00")
            val timeInterval = ((endTime.time - nowTime.time) / 1000).toInt()
            CbyLogUtils.i(
                "m3839",
                "limitSeconds = " + endTime.time / 1000 + ", nowSecond = " + nowTime.time / 1000 + ", leftTime = " + timeInterval + ", remainTime = " + remainTime
            )
            return timeInterval >= remainTime
        } catch (e: ParseException) {
            e.printStackTrace()
        }
        return false
    }
}